$('#loading').children()
    .hide()  // hide it initially
    $('#loading').ajaxStart(function() {
		$(this).find('p').html("Saving schedule...");
        $(this).children().show();
    })

    .ajaxStop(function() {
		$(this).find('p').html("Schedule updated");
    el=$(this);
        setTimeout(function(){el.children().hide();},2000);
    });
$('#recycle').click(function(){
	if($(this).attr("src")==="/resources/images/recycle_bin2.png"){
		$(this).attr("src","/resources/images/recycle_bin2active.png");
		$('.timeline > .box:not(.start)').css('cursor', "url('/resources/images/trashcan.gif'),default").bind("click.remove",function(){
			$(this).remove();
			return false;
		});
		
	}
	else{
		$(this).attr("src","/resources/images/recycle_bin2.png");
		$('.timeline > .box:not(.start)').css('cursor','').unbind("click.remove");
	}
});
jQuery('.drag > div').draggable({helper:'clone',connectToSortable:'.timeline'});
jQuery('.timeline').sortable({cancel:'.start', stop: 
	function(event, ui) {
		if(!$(this).find(':first-child').hasClass('start'))
			$(this).sortable('cancel');
	}
	}
);
$('#addScenarioButton').click(function(){

	el=$('.hiddenScenario');
	clone=el.clone();
	//$('#addScenarioButton').before(clone);
	$('#scenariosPanel').append(clone);
	
	el.removeClass("hiddenScenario");	
	$('.drag > div').draggable('destroy');
	$('.timeline').sortable('destroy');
	jQuery('.drag > div').draggable({helper:'clone',connectToSortable:'.timeline'});
	jQuery('.timeline').sortable({cancel:'.start', stop: 
		function(event, ui) {
			if(!$(this).find(':first-child').hasClass('start'))
				$(this).sortable('cancel');
		}
		});
	$( ".timeline" ).bind( "sortout", function(event, ui) {
	
	});
	
	}
);
$('div.start,div.timeline>div.delay').click(function(){
	
})
$("#scenariosPanel").click(function(event){
	var target = $(event.target);	
	if(target.is('.removeButton') || target.is('.removeButton>span')){
		target.parents('.scenario').remove();
	}
	if(target.is('div.start')){
		while(true){
			var newTime = prompt("set start time (HH:MM)", "09:00");
			if(newTime===null) break;
			if(newTime.match(/([01]\d|2[0-3]):([0-5]\d)/)){
				target.html('Time '+newTime);
				break;
			}		
		}
	}
	if(target.is('div.delay')){
		while(true){
			var newLength = prompt("set delay length (HH:MM)", "00:15");
			if(newLength===null) break;
			if(newLength.match(/\d{2}:\d{2}/)){
				target.html('Delay '+newLength);
				break;
			}		
		}
	}
	
	
}
	
);

function parseScenarios(){
	
	oj={};
	$.each($('.scenario:visible'), function(index1, value1) { 
	oj[index1]=[];
	    $.each($('.box',$(value1)), function(index2, value2) { 
	        box={}
	        name=$(value2).html().trim();
	        if(name.indexOf("Time") === 0 || name.indexOf("Delay") === 0 ){
	            spaceInd=name.indexOf(" ");
	            box.name=name.substring(0,spaceInd);
	            box.value=name.substring(spaceInd+1,name.length);
	        }
	        else{
	            box.name=name;
	        }
	        oj[index1][index2]=box
	    
	    });
	});
	return JSON.stringify(oj);
	
}

$('#saveButton').click(function(event){
	scenarios=parseScenarios();
	$.post('http://localhost:8080/blog/schedules/new/'+scenarios, function(data) {
		  
	});



});